package zm.irc.cmd;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import zm.irc.client.IrcClient;
import zm.irc.dao.MessageDao;
import zm.irc.dto.ChatMessageHistory;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.receive.IrcReceiveCmdMessage;
import zm.irc.message.send.IrcChatMessage;
import zm.irc.msgqueue.LocalMemoryMsgQueue;
import zm.irc.util.CommonUtil;


/**
 * <pre>
 * show the chat history of the query user in current channel
 * Useage : 'ch [page number] [page size]
 * page number: optional , default is 1
 * page size : optional , default is 30
 * </pre>
 */
@Service(name=CommandNameConst.CMD_CHAT_HISTORY)
public class CmdChatHistory implements IrcChatMsgCmd{
    private static final Logger log = Logger.getLogger(CmdChatHistory.class);


    private static int defaultPageNumber = 1;
    private static int defaultPageSize = 30;

    @AutoWired
    private LocalMemoryMsgQueue localMemoryMsgQueue ;

    @AutoWired
    private CommonUtil commonUtil;

    @AutoWired
    private MessageDao messageDao;

    public CmdChatHistory(){
    }


    @Override
    public boolean execute(IrcReceiveCmdMessage cmd, IrcClient client) {
        String[]  params = cmd.getCmdParams();
        int pageNumber = defaultPageNumber;
        int pageSize = defaultPageSize;
        String channel = cmd.getChannel();
        String queryBy = cmd.getFromName();
        if(params != null){
            if(params.length == 1){
                pageNumber  = Integer.valueOf(params[0]);
            }
            if(params.length > 1){
                pageSize  = Integer.valueOf(params[1]);
            }
        }

        List<ChatMessageHistory> histories = this.messageDao.queryHistory(channel, pageNumber, pageSize);
        DateTimeFormatter hhMMss = DateTimeFormatter.ofPattern("HH:mm:ss");
        if(CollectionUtils.isNotEmpty(histories)){
            StringBuffer msgHistory = new StringBuffer();
            StringBuffer msgLine = new StringBuffer();
            for(ChatMessageHistory cmh : histories){
               
                msgLine.append(cmh.getTime().format(hhMMss));
                msgLine.append("[");
                msgLine.append(cmh.getChannel());
                msgLine.append("]:");
                msgLine.append(cmh.getFromName());
                msgLine.append(" >>> ");
                msgLine.append(cmh.getContent());
                msgLine.append(" | ");
           //     msgLine.append(" \r\n");

                if(msgHistory.length() + msgLine.length() >= 510){
                    this.sendMsg(queryBy, msgHistory.toString());
                    msgHistory.delete(0, msgHistory.length() - 1);
                    
                }
                msgHistory.append(msgLine.toString());
                msgLine.delete(0, msgLine.length() - 1);
               
            }

            if(msgHistory.length() != 0){
                this.sendMsg(queryBy, msgHistory.toString());
            }
            
        }else{
            this.sendMsg(queryBy, "Today's Chat history is empty! [" + channel + "]");
        }
       
      
        return true;
    }

    private void sendMsg(String channel ,String msgStr){
        IrcChatMessage msg = new IrcChatMessage();
        msg.setChannel(channel);
        msg.setMsg(msgStr);
        this.localMemoryMsgQueue.addSendQueue(msg);
        commonUtil.slowDown(800);
    }
}
